/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7
import "DCDataClasses.js" as DCDATA

Rectangle {
    id: messagesendingwindow
    width: 300
    height: 300
    color: "#abd0ec"
    clip: true
    border.color: "#000000"
    z: 990
    visible:  false

    property string datacloudurl: ""
    property string karpoid: ""
    property string username: ""
    property string password: ""
    property string carpoolid: ""

    property variant members: []

    function show(carpoolkarpoid, memberkarpoidarray)
    {
        carpoolid = carpoolkarpoid
        members = memberkarpoidarray
        messagesendingwindow.visible = !messagesendingwindow.visible

    }

    MessageBox
    {
        id: messagebox
        property string onOkTodo: "" // closeMessageSending

        onOk:
        {
            if (onOkTodo == "closeMessageSending")
            {
                edit.text = ""
                messagesendingwindow.visible = false
            }
            onOkTodo = ""
        }

    }

    Text {
        id: msgsendtitle
        height: 25
        text: "Message to carpool's members:"
        anchors.top: parent.top
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 5
        font.pixelSize: 18
    }
    PushButton {
        id: cancelbutton
        x: 231
        y: 274
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        label: "Cancel"

        onButtonpressed:
        {
            messagesendingwindow.visible = false
        }
    }
    PushButton {
        id: sendbutton
        x: 157
        y: 274
        anchors.right: cancelbutton.left
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        label: "Send"
        onButtonpressed:
        {
            if (edit.text.length == 0)
            {
                messagebox.text = "Write the message first."
                messagebox.show("")
            }
            else
            {
                messagebox.text = "Sending the message..."
                messagebox.show("")

                var dateTime = new Date()
                var timestamp = Qt.formatDateTime ( dateTime, "yyyyMMddhhmmss" )

                var msg = new DCDATA.DCMessageOut()
                msg.karpoid = karpoid
                msg.username = username
                msg.password = password
                msg.receiverskarpoidarray = members
                msg.message = edit.text
                msg.statuschangetime = timestamp.toString()

                DCDATA.send(msg, datacloudurl,"sendmessage",sendmessagecb)

            }
        }
    }



    function sendmessagecb(response)
    {
        console.log("sendmessagecb: " + JSON.stringify(response))

        if (response.isError)
        {
            messagebox.text = "Failed: " + response.errorString
        }
        else
        {
            messagebox.text = "Message sent."
            messagebox.onOkTodo = "closeMessageSending"
        }

    }

    Rectangle
    {
        id: flickrect
        color: "#f2f392"
        border.width: 1
        border.color: "#1d2b44"
        anchors.bottom: cancelbutton.top
        anchors.bottomMargin: 10
        anchors.top: msgsendtitle.bottom
        anchors.topMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10

        Flickable
        {
            id: flick

            x: 5
            width: flickrect.width - 5
            y: 5
            height: flickrect.height - 5

            contentWidth: edit.paintedWidth
            contentHeight: edit.paintedHeight
            clip: true

            function ensureVisible(r)
            {
                if (contentX >= r.x)
                    contentX = r.x;
                else if (contentX+width <= r.x+r.width)
                    contentX = r.x+r.width-width;
                if (contentY >= r.y)
                    contentY = r.y;
                else if (contentY+height <= r.y+r.height)
                    contentY = r.y+r.height-height;
            }

            TextEdit {
                id: edit
                width: flick.width
                height: flick.height
                cursorVisible: true
                focus: true
                wrapMode: TextEdit.WordWrap
                font.pixelSize: 12
                onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
            }
        }
    }

}
